package com.sebklebes.radar.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Toast;
import com.sebklebes.radar.httpclient.HttpClient;
import com.sebklebes.radar.radar.RadarService;

public class DialogFactory {

	private static final String TAG = DialogFactory.class.getName();
	public static final String DIALOGS = "com.sebklebes.android-radar.dialogs";
	public static final String MESSAGE = "com.sebklebes.android-radar.message";

	private static Dialog dialog;

	public static Dialog getDialog(int action, final Activity contextActivity, String message) {
		switch (action) {
			case DialogTypes.SERVICE_NOT_RUNNING: {
				if (message == null) {
					message = "The radar service is stopped. Do you want to turn it on?";
				}

				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						contextActivity);
				alertDialogBuilder.setTitle("RADAR INACTIVE");
				alertDialogBuilder
						.setMessage(message);
				alertDialogBuilder.setPositiveButton("OK",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								contextActivity.startService(new Intent(
										contextActivity, RadarService.class));
								dialog.dismiss();
								contextActivity.finish();
							}
						});
				alertDialogBuilder.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
								contextActivity.finish();
							}
						});

				dialog = alertDialogBuilder.create();
				return dialog;
			}

			case DialogTypes.SEND_RADAR: {
				if (message == null) {
					message = "Send radar location to server?";
				}

				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						contextActivity);
				alertDialogBuilder.setTitle("RADAR DETECTED");
				alertDialogBuilder.setMessage("Send radar location to server?");
				alertDialogBuilder.setPositiveButton("OK",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								HttpClient
										.addRadar(RadarService.cachedLocation);
								dialog.dismiss();
								Toast.makeText(contextActivity, "Radar location saved.",
										Toast.LENGTH_SHORT).show();
								contextActivity.finish();
							}
						});

				alertDialogBuilder.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
								contextActivity.finish();
							}
						});

				dialog = alertDialogBuilder.create();
				return dialog;
			}
			case DialogTypes.RADAR_ALERT: {
				if (message == null) {
					message = "Radar detected in a 1km range!";
				}

				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						contextActivity);
				alertDialogBuilder.setTitle("RADAR ALERT");
				alertDialogBuilder.setMessage(message);
				alertDialogBuilder.setNegativeButton("Close",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
								contextActivity.finish();
							}
						});

				dialog = alertDialogBuilder.create();
				return dialog;
			}
		}
		return dialog;
	}
}
